#include "actiondebugform.h"
#include "ui_actiondebugform.h"
#include "util/utils.h"
#include "form/homewindow.h"

ActionDebugForm::ActionDebugForm(BaseForm *parent) :
    BaseForm(parent),
    ui(new Ui::ActionDebugForm),
    isRunning(false)
{
    ui->setupUi(this);
    initWindow();
}

ActionDebugForm::~ActionDebugForm()
{
    delete ui;
}

bool ActionDebugForm::isRunDebug() const
{
    return isRunning;
}

void ActionDebugForm::setHomeWindowPtr(HomeWindow *_homePtr)
{
    this->homeWindowPtr = _homePtr;
}

void ActionDebugForm::initWindow()
{

}

void ActionDebugForm::on_btnRun_clicked()
{
    this->homeWindowPtr->startRunningTest();

    ///开始闭环控制
    callback(Utils::formatCommand("SET", 0.0));
    Utils::delay(1);
    float up = ui->spWaveUp->text().toFloat();
    float down = ui->spWaveDown->text().toFloat();
    float res = fabs(up - down) / 2;
    callback(Utils::formatCommand("ZZF", res));
    Utils::delay(1);
    callback(Utils::formatCommand("MAX", up));
    Utils::delay(1);
    float freq = ui->leDebugFreq->text().toFloat();
    callback(Utils::formatCommand("ZPL", freq));

    float num = ui->leDebugNum->text().toFloat();
    callback(Utils::formatCommand("NUM", num));
    Utils::delay(1);

    float stSpeed = ui->leStartSpeed->text().toFloat();
    stSpeed = stSpeed/up;
    stSpeed = stSpeed/freq;
    callback(Utils::formatCommand("D_V", stSpeed));
    Utils::delay(1);

    QString ctrl = ui->cbWaveCtrl->currentText();
    if(ctrl == "正玄波")
    {
        callback("{BFS[Z]}");
    }
    else
    {
        callback("{BFS[S]}");
    }

    Utils::delay(1);

    if(ui->rbDistanceCtrl->isChecked())
    {
        callback("{TRE[W]}");
    }
    else
    {
        callback("{TRE[L]}");
    }

    isRunning = true;
}

void ActionDebugForm::on_btnOilStart_clicked()
{
    /// 伺服启动
    QString cmd = "{CE1[Y]}";
    callback(cmd);

    ui->lbDebugOilState->setText("油源启动");
    ui->lbDebugOilState->setStyleSheet("background-color:green;");
    ui->btnOilStart->setEnabled(false);
    ui->btnOilStop->setEnabled(true);
}

void ActionDebugForm::on_btnOilStop_clicked()
{
    /// 伺服关闭
    QString cmd = "{CE1[N]}";
    callback(cmd);

    ui->lbDebugOilState->setText("油源停止");
    ui->lbDebugOilState->setStyleSheet("background-color:red;");
    ui->btnOilStart->setEnabled(true);
    ui->btnOilStop->setEnabled(false);
}

void ActionDebugForm::on_btnKeepPress_clicked()
{
    ///保压
    QString cmd = "{02[00]}";
    callback(cmd);
}

void ActionDebugForm::on_btnLosePress_clicked()
{
    ///卸压
    QString cmd = "{02[08]}";
    callback(cmd);
}

void ActionDebugForm::on_btnCharge_clicked()
{

}

void ActionDebugForm::on_btnDisplacement_clicked()
{
    QString cmd = "{QLI[C]}";
    callback(cmd);
}

void ActionDebugForm::on_btnLoopUp_clicked()
{
    /// 开环点动给定上升。
    QString cmd = ui->leLoopUp->text();
    callback(Utils::formatCommand("SET", cmd.toFloat()));
}

void ActionDebugForm::on_btnLoopDown_clicked()
{
    /// 开环点动给定下降。
    QString cmd = ui->leLoopDown->text();
    float val = 0.0 - cmd.toFloat();
    callback(Utils::formatCommand("SET", val));
}

//停止闭环控制
void ActionDebugForm::on_btnStopDebug_clicked()
{
    QString cmd = "{SCH[W]}";
    callback(cmd);
    Utils::delay(10);
    cmd = "{RUN[T]}";
    callback(cmd);

    isRunning = false;
}

///停止归零位
void ActionDebugForm::on_btnDebugStopReset_clicked()
{
    callback(Utils::formatCommand("SET", 0));
}

void ActionDebugForm::on_btnDebugClose_clicked()
{
    this->close();
}
